package com.dhl.fin.api.controller;

import com.dhl.fin.api.common.controller.CommonController;
import com.dhl.fin.api.common.dto.ApiResponse;
import com.dhl.fin.api.common.util.MapUtil;
import com.dhl.fin.api.common.util.StringUtil;
import com.dhl.fin.api.common.util.WebUtil;
import com.dhl.fin.api.domain.RemindTask;
import com.dhl.fin.api.dto.RemindTypeInfoDTO;
import com.dhl.fin.api.enums.RelationshipTypeEnum;
import com.dhl.fin.api.service.LoginAccountService;
import com.dhl.fin.api.service.RemindTaskService;
import com.nlf.calendar.Lunar;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author CuiJianbo
 * @since 2025/3/16
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@Slf4j
@RequestMapping("remind")
public class RemindTaskController extends CommonController<RemindTask> {

    @Autowired
    private RemindTaskService remindTaskService;

    @Autowired
    private LoginAccountService loginAccountService;


    @ResponseBody
    @GetMapping("initdata")
    public ApiResponse remindInitData() throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();
        int count = loginAccountService.getAccountAllowCount();
        int taskCount = remindTaskService.taskCount(unicode);
        List<RemindTypeInfoDTO> remindTypeInfoDTOList = remindTaskService.remindInitData();
        return ApiResponse.success(MapUtil.builder()
                .add("count", count)
                .add("taskCount", taskCount)
                .add("remindTypes", remindTypeInfoDTOList)
                .build());
    }

    @ResponseBody
    @GetMapping("allowcount")
    public ApiResponse allowAccount() throws Exception {
        return ApiResponse.success(loginAccountService.getAccountAllowCount());
    }


    @ResponseBody
    @PostMapping("createNewRemindTask")
    public ApiResponse createNewRemindTask(@RequestBody RemindTask remindTask) throws Exception {
        remindTaskService.createNewRemindTask(remindTask);
        return ApiResponse.success();
    }


    @ResponseBody
    @GetMapping("deleteRemind/{id}")
    public ApiResponse deleteRemind(@PathVariable Long id) throws Exception {
        remindTaskService.deleteRemind(id);
        return ApiResponse.success();
    }

    @ResponseBody
    @GetMapping("nexttime")
    public ApiResponse getNextExecuteTime(RemindTask remindTask) throws Exception {
        return ApiResponse.success(remindTaskService.getNextFiveExecuteTime(remindTask));
    }

    @ResponseBody
    @GetMapping("form/initdata")
    public ApiResponse getInitData() throws Exception {
        Lunar lunarNow = Lunar.fromDate(new Date());
        String unicode = WebUtil.getLoginUser().getUuid();
        String lunarDate = StringUtil.join(lunarNow.getMonthInChinese(), "月", lunarNow.getDayInChinese());
        List<String> relations = Arrays.stream(RelationshipTypeEnum.values()).map(RelationshipTypeEnum::getName).collect(Collectors.toList());
        Boolean isNewUser = remindTaskService.isNewUser(unicode);
        return ApiResponse.success(MapUtil.builder()
                .add("lunarDate", lunarDate)
                .add("relations", relations)
                .add("isNewUser", isNewUser)
                .build());
    }

    @ResponseBody
    @GetMapping("isnewuser")
    public ApiResponse checkIsNewUser() throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();
        return ApiResponse.success(remindTaskService.isNewUser(unicode));
    }


}
